/**
 * 人卡关系管理初始化
 */
var UserCard = {
    id: "UserCardTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
UserCard.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        // {title: 'id', field: 'id', align: 'center', valign: 'middle',width:'50px',hidden:true},
        {title: '卡號', field: 'cardNo', visible: true, align: 'center', valign: 'middle'},
        {title: '人员编号', field: 'userInfo.id', visible: true, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'userInfo.name', visible: true, align: 'center', valign: 'middle'},
        {title: '部门编号', field: 'userInfo.deptInfo.id', visible: true, align: 'center', valign: 'middle'},
        {title: '部门名称', field: 'userInfo.deptInfo.fullname', visible: true, align: 'center', valign: 'middle'},
        {title: '发卡日期', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
        {title: '卡状态', field: 'isUnbinding', visible: true, align: 'center', valign: 'middle',
            formatter:function(data,row){
                if(row.isUnbinding==0){
                    return "已解绑"
                }else{
                    return "已绑定"
                }
            }}
    ];
};



/**
 * 检查是否选中
 */
UserCard.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        UserCard.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加人卡关系
 */
UserCard.openAddUserCard = function () {
    var index = layer.open({
        type: 2,
        title: '添加人卡关系',
        area: ['85%', '85%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/door/USER/CARD/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看人卡关系详情
 */
UserCard.openUserCardDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统参数详情',
            area: ['85%', '85%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/door/USER/CARD/edit/' + UserCard.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除人卡关系
 */
UserCard.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/USER/CARD/delete", function (data) {
            Feng.success("删除成功!");
            UserCard.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询人卡关系列表
 */
UserCard.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    UserCard.table.refresh({query: queryData});
};
/**
 * 重置查询条件
 */
UserCard.reset = function () {
    $('#name').val('');
    this.search();
};

$(function () {
    var defaultColunms = UserCard.initColumn();
    var table = new BSTable(UserCard.id, "/door/USER/CARD/list", defaultColunms);
    table.setPaginationType("server");
    UserCard.table = table.init();
});

UserCard.batchAdd = function () {
    var accountNo = $("#accountNo").val();
    if (accountNo == null || accountNo == "") {
        Feng.info("请输入数量")
        return false;
    }
    var ajax = new $ax(Feng.ctxPath + "/mgr/batchAdd", function (data) {
        Feng.success("批量生成用戶成功!");
        UserCard.table.refresh();
    }, function (data) {
        Feng.error("批量生成用戶失败!");
    });
    ajax.set("accountNo", accountNo);
    ajax.start();
}

/**
 * 冻结用户账户
 * @param userId
 */
UserCard.freezeAccount = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var ajax = new $ax(Feng.ctxPath + "/door/USER/CARD/freeze", function (data) {
            Feng.success("冻结成功!");
            UserCard.table.refresh();
        }, function (data) {
            Feng.error("冻结失败!" + data.responseJSON.message + "!");
        });
        ajax.set("userId", userId);
        ajax.start();
    }
};

/**
 * 解除冻结用户账户
 * @param userId
 */
UserCard.unfreeze = function () {
    if (this.check()) {
        var userId = this.seItem.id;
        var ajax = new $ax(Feng.ctxPath + "/door/USER/CARD/unfreeze", function (data) {
            Feng.success("解除綁定成功!");
            UserCard.table.refresh();
        }, function (data) {
            Feng.error("解除綁定失败!");
        });
        ajax.set("id", userId);
        ajax.start();
    }
}
